//
//  AddDeviceViewController.m
//  AILock
//
//  Created by 谭亚明on 2020/6/17.
//  Copyright © 2020 tym. All rights reserved.
//

#import "AddDeviceViewController.h"
#import "TYMBaseTableView.h"
#import "Sizes.h"
#import "SeleDevTypeViewController.h"
#import "AddDeviceCell.h"
#import "EquipmentService.h"
@interface AddDeviceViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,weak) TYMBaseTableView *deviceTableView;

@property(nonatomic,strong) EquipmentService  *services;
@property(nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation AddDeviceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTitle:@"添加设备"];
    [self addBackBtnFunc];
    [self getDataArry];
    [self initTableView];
}

-(NSMutableArray*)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

-(void)getDataArry{
    [self.services getFindequipmenttypelistcompletion:^(NSError * _Nonnull error, NSArray<EquipmentTypeModel *> * _Nonnull array) {
        [self.dataArray addObjectsFromArray:array];
        [self.deviceTableView reloadData];
    }];
}

-(EquipmentService*)services{
    if (_services == nil) {
        _services = [[EquipmentService alloc] init];
    }
    return _services;
}

-(void)initTableView{
    
    TYMBaseTableView *table = [[TYMBaseTableView alloc] initWithFrame:CGRectMake(0,0 , ScreenWidth, ScreenHeight - NavigateonHeight - SafeBottomMargin)];
    table.separatorInset =UIEdgeInsetsMake(5,0,0, 0);
    table.delegate = self;
    table.dataSource = self;
    [self.view addSubview:table];
    [table registerNib:[UINib nibWithNibName:NSStringFromClass([AddDeviceCell class]) bundle:nil] forCellReuseIdentifier:[AddDeviceCell identifier]];
    self.deviceTableView = table;
}

#pragma  mark - tableView 代理
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [AddDeviceCell cellForHeight];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddDeviceCell *cell = [tableView dequeueReusableCellWithIdentifier:[AddDeviceCell identifier] forIndexPath:indexPath];
    cell.typeModel = self.dataArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SeleDevTypeViewController *seleVC = [[SeleDevTypeViewController alloc] init];
    seleVC.equmenModel = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:seleVC animated:YES];
}
@end
